package cn.schoolwow.util.module.query.instance.service;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.reflection.QueryReflection;
import cn.schoolwow.util.module.query.instance.domain.InstanceQueryOption;

import java.lang.reflect.Field;

public class GetInstanceFieldValueFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        String fieldName = flowContext.checkData("fieldName", String.class);
        InstanceQueryOption instanceQueryOption = flowContext.checkInstanceData(InstanceQueryOption.class);

        Field field = QueryReflection.newQuery(instanceQueryOption.instance.getClass())
                .execute()
                .getField(fieldName);
        Object fieldValue = field.get(instanceQueryOption.instance);
        flowContext.putReturnData("fieldValue", fieldValue);
    }

    public String name() {
        return "通过反射获取实例指定字段的值";
    }
}
